   
   function initJqgrid(){
	   //$("#list1").tableDnD({scrollAmount:0});
	   initAddUserDialog();
	   initUserTypeAutocomplete();
$("#Product_list1").jqGrid({
   	url:'../productHandle/editProduct.jsp',
	datatype: "xml",
   	colNames:['产品ID','产品名称','产品价格','产品类型', '产品描述','删除'],
   	colModel:[
		{name:'Product_ID',index:'Product_ID', width:75, },
   		{name:'Product_Name',index:'Product_Name',editable:true, width:75},
   		{name:'Product_Price',index:'Product_Price',align:"center", width:50,editable:true,},
		{name:'Product_Type',index:'Product_Type',align:"center", width:50},
   		{name:'Product_DESC',index:'Product_DESC', editable:true },
		{name:'del',index:'del', width:50,align:"center",formatter:FmatterDel},	
   	],
	
	//scroll: true,
   	rowNum:15,
   	autowidth: true,
   	rowList:[10,20,30],
	height:400,
	prmNames:{page:'page',rows:'rows',order: 'sord', search:'_search', nd:'nd', npage:null},
	//postData:{page:'page',rows:'rows', sort: 'sidx',order: 'sordsdf'},
   	pager: jQuery('#Product_pager1'),
   	sortname: 'Product_Price',
    viewrecords: true,
    sortorder: "desc",
    caption:"XML Example",	
	forceFit : true,
	cellEdit: true,
	cellsubmit: 'clientArray',
	afterEditCell: function (id,name,val,iRow,iCol){
		if(name=='Called_Time') {
			jQuery("#"+iRow+"_"+name,"#list1").datepicker({dateFormat:"yy.mm.dd"});
		}
	},
	afterSaveCell : function(rowid,name,val,iRow,iCol) {
		var myrow = jQuery('#Product_list1').jqGrid('getRowData',rowid);
		modifyProduct(myrow.Product_ID,name+"="+val);
	},
	loadCompele:function(){alert("compele")},
	loadError:function(XMLHttpRequest, textStatus, errorThrown) {
      alert("status:"+XMLHttpRequest.status+"readyState:"+XMLHttpRequest.readyState+"text:"+XMLHttpRequest.responseText);
                    },
	onSelectRow: function(id){    
      
   },   
	gridComplete: function() {
 
    }, 
	//此事件在点击表格特定单元格时发生。rowid 为行ID；iCol 为列索引；cellcontent 为单元格中内容；e 点击事件对象
	onCellSelect:function(rowid,iCol,cellcontent,e){
		if(iCol==5){
			var aa= window.confirm("是否删除产品(这将删除产品对应所有数据)！");
			if (aa) {
		    var myrow = jQuery('#Product_list1').jqGrid('getRowData',rowid);
			delUser(myrow.Product_ID);
			}
			
			}
			
		},
	editurl:"../productHandle/editProduct.jsp"
}).navGrid('#Product_pager1',{edit:true,add:false,del:false,search: false }).navButtonAdd('#Product_pager1',{   
   caption:"添加",    
   buttonicon:"ui-icon-add",    
   onClickButton: function(){    
    $('#Product-form').dialog('open').children('form')[0].reset();  
   },    

   position:"last"  
}).navButtonAdd('#pager1',{   
   caption:"删除",    
   buttonicon:"ui-icon-del",    
   onClickButton: function(){    
     alert("Adding Row");   
   },    

   position:"last"  
}).navSeparatorAdd("#Product_pager1",{sepclass : "ui-separator",sepcontent: ''});	
//jQuery("#list1").jqGrid('inlineNav',"#pager1");
	   }
	
	function  modifyProduct(Product_ID,url){
			$.ajax({
   type: "POST",
   async: false,
   dataType: "xml",
   url: "../productHandle/editProduct.jsp?oper=edit",
   data: encodeURI("Product_ID="+Product_ID+"&"+url),
   success: function(xmlResponse){
    //getStart(xmlResponse);
	var statuc=$(xmlResponse).find("statuc").text();	
	if(statuc=="true"){
		Toalert($(xmlResponse).find('msg').text());
		//$("#User_list1").trigger("reloadGrid");
		}else{
		Toalert($(xmlResponse).find('msg').text());
		}
 	},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert("status:"+XMLHttpRequest.status+"readyState:"+XMLHttpRequest.readyState+"text:"+XMLHttpRequest.responseText);
                    },
 });
		}
	   
	function FmatterUserType(cellvalue, options, rowObject){
		  if(cellvalue=='1'){
			  return "管理员";
		  }else if(cellvalue=='2'){
			  return "业务员";
		  }else if(cellvalue=='3'){
			  return "客户";
		  }else if(cellvalue=='4'){
			  return "送货员";
		  }
		}
	function FmatterDel(cellvalue, options, rowObject){
			return "删除";
		}
		
	function delUser(User_ID){
		$.ajax({
   type: "POST",
   async: false,
   dataType: "xml",
   url: "../productHandle/delectProduct.jsp",
   data: encodeURI("Product_ID="+User_ID),
   success: function(xmlResponse){
    //getStart(xmlResponse);
	var statuc=$(xmlResponse).find("statuc").text();	
	if(statuc=="true"){
		Toalert($(xmlResponse).find('msg').text());
		$("#list1").trigger("reloadGrid");
		}else{
		Toalert($(xmlResponse).find('msg').text());
		}
 	},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert("status:"+XMLHttpRequest.status+"readyState:"+XMLHttpRequest.readyState+"text:"+XMLHttpRequest.responseText);
                    },
 });
		}
  function addProduct(Add_Product_Name,Add_Product_Price,Add_Product_Type,Add_Product_DESC){
	  	$.ajax({
   type: "POST",
   async: false,
   dataType: "xml",
   url: "../productHandle/addProduct.jsp",
   data: encodeURI("Product_Name="+Add_Product_Name+"&Product_Price="+Add_Product_Price+"&Product_Type="+Add_Product_Type+"&Product_DESC="+Add_Product_DESC),
   success: function(xmlResponse){
    //getStart(xmlResponse);
	var statuc=$(xmlResponse).find("statuc").text();	
	if(statuc=="true"){
		AddUserMsg($(xmlResponse).find('msg').text());
		$("#Product_list1").trigger("reloadGrid");
		}else{
		AddUserMsg($(xmlResponse).find('msg').text());
		}
 	},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert("status:"+XMLHttpRequest.status+"readyState:"+XMLHttpRequest.readyState+"text:"+XMLHttpRequest.responseText);
                    },
 });
	  }
 function AddUserMsg(text){
	 
	 $( "#Add_User_Msg" ).text( text )
				.addClass( "ui-state-highlight" );
			setTimeout(function() {
				$( "#Add_User_Msg" ).removeClass( "ui-state-highlight", 1500 );
			}, 500 );
	 }
 function Toalert(text){
	 alert(text);
	 }
	 
	 //初始化登录对话框
function initAddUserDialog(){
	$( "#Product-form" ).dialog({
			autoOpen: false,
			height: 400,
			width: 350,
			modal: true,
			buttons: {
				"添加": function() {
					var type=$("#Add_Product_Type").autocomplete("option","selectedType");
					if(type=="全部"){
						AddUserMsg("请选择正确的产品类型");
						return;
						}
					addProduct($( "#Add_Product_Name").val(),$( "#Add_Product_Price").val(),type,$( "#Add_Product_DESC").val());
				},
				//"取消": function() {
				//	$( this ).dialog( "close" );
				//}
			},
			"取消": function() {
				//allFields.val( "" ).removeClass( "ui-state-error" );
			}
		});
}

function initUserTypeAutocomplete(){
		$( "#Add_Product_Type" ).autocomplete({
			source: function (request, response) {            
			 request.page = "1";  
			 request.rows= "100"; 
			 request.Product_Type=request.term;
				 $(this).autocomplete({
									 selectedType:request.term,
									 });
				$.ajax({
					url: "../producttypeinfoHandle/searchProductType.jsp",
					data: request,
					dataType: "json",
					success: function( data, status ) {
					
							response( data );
						
					},
					error: function() {
					
							response( [] );
						
					}
				});
			 },
			minLength: 0,
			selectedType:0,
			select: function( event, ui ) {
				//log( ui.item ?
				//	"Selected: " + ui.item.value + " aka " + ui.item.id :
				//	"Nothing selected, input was " + this.value );
					$(this).autocomplete({
									 selectedType:ui.item.value,
					});
			}
		});
		$( "#ProductType" ).autocomplete({
			//source: "../userHandle/queryUser_Type.jsp",
			minLength: 0,
			selectedType:0,
			 source: function (request, response) {            
			 request.page = "1";  
			 request.rows= "100"; 
			 request.Product_Type=request.term;
				 $(this).autocomplete({
									 selectedType:request.term,
									 });
				$.ajax({
					url: "../producttypeinfoHandle/searchProductType.jsp",
					data: request,
					dataType: "json",
					success: function( data, status ) {
					
							response( data );
						
					},
					error: function() {
					
							response( [] );
						
					}
				});
			 },
			select: function( event, ui ) {
				//log( ui.item ?
				//	"Selected: " + ui.item.value + " aka " + ui.item.id :
				//	"Nothing selected, input was " + this.value );
				//alert(ui.item.id);
				 findUser(ui.item.value);
			}
		});
	}
function initfindUser(){
			$("#find_Product").click(
		    function(){
	 findUser();
			}
		);
	}
	function findUser(ProductType){
		/*获取select 选中的 text : 
	$("#ddlRegType").find("option:selected").text(); 
	获取select选中的 value: 
	$("#ddlRegType ").val(); 
	获取select选中的索引: 
	$("#ddlRegType ").get(0).selectedIndex; 
	设置select: 
	设置select 选中的索引： 
	$("#ddlRegType ").get(0).selectedIndex=index;//index为索引值*/
	//alert("userid="+$( "#userid").val()+"  username="+$( "#username").val()+"  usertype="+$( 			"#usertype").get(0).selectedIndex);
var ProductID=encodeURIComponent($( "#ProductID").val());
var ProductName=encodeURIComponent($( "#ProductName").val());
if(empty(ProductType)){
    ProductType=$("#ProductType").val();
}
ProductType=encodeURIComponent(ProductType);
$("#Product_list1").jqGrid('setGridParam',{ url: '../productHandle/editProduct.jsp',
postData:{Product_ID:ProductID,Product_Name:ProductName,Product_Type:ProductType}
}).trigger('reloadGrid'); 
		}
		
			  //自定义序列号表单
		  function form_serialize(expr){
			  var url="";
			  $(expr).find("[type='text']").map(function(){
								if(url!=''){
									url+="&";
									}
									var v=$(this).val();
									url+=decodeURI($(this).attr("name")+"="+v);
													  }
							).get();
			  alert(url);
			  return url;
			  }
// JavaScript Document
// JavaScript Document